package com.yiren.manager.service.impl.elder;

import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.yiren.core.service.GenericServiceImpl;
import com.yiren.core.utils.Favorites;
import com.yiren.entity.YrElderWallet;
import com.yiren.entity.YrElderWalletRechargeRecord;
import com.yiren.entity.YrPaper;
import com.yiren.entity.YrPaperRecord;
import com.yiren.manager.repository.cost.YrElderWalletRechargeRecordRepository;
import com.yiren.manager.repository.cost.YrPaperRecordRepository;
import com.yiren.manager.repository.cost.YrPaperRepository;
import com.yiren.manager.repository.elder.YrElderWalletRepository;
import com.yiren.manager.service.iface.elder.YrElderWalletService;

@Service
public class YrElderWalletServiceImpl extends GenericServiceImpl<YrElderWallet, String> implements YrElderWalletService{

	@Autowired
	private YrElderWalletRechargeRecordRepository elderWalletRechargeRecordRepository;
	
	@Autowired
	private YrPaperRecordRepository paperRecordRepository;
	
	@Autowired
	private YrPaperRepository paperRepository;
	
	@Autowired
	public YrElderWalletServiceImpl(YrElderWalletRepository repository) {
		super(repository);
	}

	@Transactional
	public void executeRecharge(Favorites favorites) {
		YrElderWallet elderWallet = favorites.getFavorite(YrElderWallet.class);
		YrElderWalletRechargeRecord elderWalletRechargeRecord = favorites.getFavorite(YrElderWalletRechargeRecord.class);
		YrPaperRecord paperRecord = favorites.getFavorite(YrPaperRecord.class);
		YrPaper paper = favorites.getFavorite(YrPaper.class);
		
		this.repository.save(elderWallet);
		elderWalletRechargeRecord = elderWalletRechargeRecordRepository.save(elderWalletRechargeRecord);
		paperRecord.setRelationId(elderWalletRechargeRecord.getElderWalletRechargeRecordId()+"-walletRecharge");
		
		paperRecordRepository.save(paperRecord);
		paperRepository.save(paper);
	}

}
